package cn.sjxy.apiAdmin;

import cn.sjxy.baseLib.AjaxResult;
import cn.sjxy.baseLib.SjuException;
import cn.sjxy.config.mvcExtend.NeedAdminLogin;
import cn.sjxy.mapper.Mp_BookType;
import cn.sjxy.model.Md_BookType;
import cn.sjxy.service.Sv_BookType;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * 管理员端 - 图书类型管理API
 */
@RestController
@Validated
@Api(tags = "管理员端-图书类型管理")
public class Ad_BookType {

    @Autowired
    private Mp_BookType mp_bookType;

    @Autowired
    private Sv_BookType sv_bookType;

    /**
     * 获取图书类型列表（分页）
     *
     * @param typeName  类型名称（模糊查询）
     * @param pageIndex 页码
     * @param pageSize  每页数量
     * @return
     */
    @GetMapping(value = "/admin/bookType/getTable")
    @ApiOperation("获取图书类型列表")
    @NeedAdminLogin
    public AjaxResult getTable(
            @RequestParam(required = false) String typeName,
            @RequestParam(defaultValue = "1") Integer pageIndex,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        // 构建查询条件
        QueryWrapper<Md_BookType> wrapper = new QueryWrapper<>();

        // 类型名称模糊查询
        if (typeName != null && !typeName.trim().isEmpty()) {
            wrapper.like("typeName", typeName);
        }

        // 按显示顺序倒序
        wrapper.orderByDesc("showOrder");

        // 分页查询
        IPage<Md_BookType> page = new Page<>(pageIndex, pageSize);
        page = mp_bookType.selectPage(page, wrapper);

        // 返回结果
        AjaxResult result = AjaxResult.success("查询成功");
        result.addTable(page.getRecords());
        result.addPage(page);
        return result;
    }

    /**
     * 获取图书类型详情
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/admin/bookType/getDetail")
    @ApiOperation("获取图书类型详情")
    @NeedAdminLogin
    public AjaxResult getDetail(@NotNull(message = "ID不能为空") Long id) {
        Md_BookType bookType = mp_bookType.selectById(id);
        AjaxResult result = AjaxResult.success("查询成功");
        result.addKey("entry", bookType);
        return result;
    }

    /**
     * 新增图书类型
     *
     * @param bookType
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/admin/bookType/insert")
    @ApiOperation("新增图书类型")
    @NeedAdminLogin
    public AjaxResult insert(@RequestBody Md_BookType bookType) throws Exception {
        // 检查类型编号是否已存在
        if (sv_bookType.isTypeCodeExist(bookType.getTypeCode(), null)) {
            SjuException.appEx("类型编号已存在");
        }

        if (bookType.getShowOrder() == null) {
            bookType.setShowOrder(100);
        }

        int result = mp_bookType.insert(bookType);
        if (result > 0) {
            return AjaxResult.success("新增成功");
        } else {
            SjuException.appEx("新增失败");
            return null;
        }
    }

    /**
     * 修改图书类型
     *
     * @param bookType
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/admin/bookType/update")
    @ApiOperation("修改图书类型")
    @NeedAdminLogin
    public AjaxResult update(@RequestBody Md_BookType bookType) throws Exception {
        // 检查类型编号是否已存在（排除自己）
        if (sv_bookType.isTypeCodeExist(bookType.getTypeCode(), bookType.getId())) {
            SjuException.appEx("类型编号已存在");
        }

        int result = mp_bookType.updateById(bookType);
        if (result > 0) {
            return AjaxResult.success("修改成功");
        } else {
            SjuException.appEx("修改失败");
            return null;
        }
    }

    /**
     * 删除图书类型
     *
     * @param typeCode
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/admin/bookType/delete")
    @ApiOperation("删除图书类型")
    @NeedAdminLogin
    public AjaxResult delete(@NotBlank(message = "类型编号不能为空") String typeCode) throws Exception {
        sv_bookType.deleteBookType(typeCode);
        return AjaxResult.success("删除成功");
    }
}

